import fs from 'fs'
const packageJson = JSON.parse(fs.readFileSync('./package.json', 'utf-8'));

function incrementVersion() {
    const version = packageJson.version;
    const parts = version.split('.');
    const major = parseInt(parts[0]);
    const minor = parseInt(parts[1]);
    const patch = parseInt(parts[2]);

    let newPatch, newMinor, newMajor;

    if (patch < 9) {
        newPatch = patch + 1;
        newMinor = minor;
        newMajor = major;
    } else if (minor < 9) {
        newPatch = 0;
        newMinor = minor + 1;
        newMajor = major;
    } else {
        newPatch = 0;
        newMinor = 0;
        newMajor = major + 1;
    }

    const newVersion = `${newMajor}.${newMinor}.${newPatch}`;

    packageJson.version = `${newVersion}-${getCurrentDate()}`;
    fs.writeFileSync('./package.json', JSON.stringify(packageJson, null, 2));

    console.log(`New version: ${packageJson.version}`);
}

function getCurrentDate() {
    const currentDate = new Date();
    const year = currentDate.getFullYear();
    const month = padZero(currentDate.getMonth() + 1);
    const day = padZero(currentDate.getDate());

    return `${year}${month}${day}`;
}

function padZero(number) {
    return number.toString().padStart(2, '0');
}

incrementVersion();